<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="rolesList">
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-row v-for="(item1, i1) in scope.row.children" :key="item1.id" :class="['tagbb', i1 === 0? 'tagbt' : '', 'vcenter']">
                <!-- 一级权限 -->
                <el-col :span="5">
                  <el-row>
                      <el-tag closable @close="removeAuth(scope.row, item1.id)">{{item1.authName}}</el-tag>
                      <i class="el-icon-caret-right"></i>
                  </el-row>
                </el-col>
                <!-- 二级权限及三级权限 -->
                <el-col :span="19">
                    <el-row :class="['tagtb', i2 === 0? '' : 'tagbt', 'vcenter']" v-for="(item2, i2) in item1.children" :key="item2.id">
                        <el-col :span="6">
                            <el-tag type="success" closable @close="removeAuth(scope.row, item2.id)">{{item2.authName}}</el-tag>
                            <i class="el-icon-caret-right"></i>
                        </el-col>
                        <el-col :span="18">
                            <el-tag @close="removeAuth(scope.row, item3.id)" type="warning" v-for="(item3) in item2.children" :key="item3.id" closable>{{item3.authName}}</el-tag>
                        </el-col>
                    </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="roleName" label="角色名称"></el-table-column>
          <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
          <el-table-column label="操作">
            <template>
                <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                <el-button type="warning" icon="el-icon-setting" size="mini" @click="settingDialogVisible = true">分配权限</el-button>
            </template>
          </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限的对话框 -->
    <el-dialog
    title="分配权限"
    :visible.sync="settingDialogVisible"
    width="50%">
      <el-tree :data="rightsList" :props="defaultProps"></el-tree>
      <span slot="footer">
          <el-button @click="settingDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="settingDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Roles',
  data () {
    return {
      rolesList: [],
      settingDialogVisible: false,
      rightsList: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      }
    }
  },
  methods: {
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) return this.$message.error('获取角色列表失败')
      this.rolesList = res.data
      console.log(this.rolesList)
    },
    async removeAuth (role, id) {
      const confirm = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirm === 'cancel') return this.$message.info('已经取消删除')
      const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${id}`)
      if (res.meta.status !== 200) return this.$message.error('取消权限失败')
      role.children = res.data
    },
    async getRightsList () {
      const { data: res } = await this.$http.get('rights/tree')
      console.log(res)
    }
  },
  mounted () {
    this.getRolesList()
  }
}
</script>

<style>
.el-tag {
  margin: 7px;
}

.tagbb {
  border-bottom: 1px solid #eee;
}

.tagbt {
    border-top: 1px solid #eee;
}

.vcenter {
    display: flex;
    align-items: center;
}
</style>
